home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 5.3 KB | 201 lines | [TEXT/ttxt] |
- --<<<
-
- -- Filename: stnclizr.sx
-
- -- Other Files Required: None
-
- -- Purpose:
- -- A ScriptX painting presenter, Stencilizer handles mouse events
- -- to perform basic bit painting operations. It caches results of
- -- drawing in a bitmap, which could be saved in aStorageContainer.
- -- The Stencilizer class provides instance variables that could be
- -- used with tool palettes to implement user selection of brushshape,
- -- width, and color.
-
- -- Demonstrates classes from:
- -- 2D Graphics: Stencil, Oval, Brush, Color, BitmapSurface
- -- Presenters: TwoDPresenter (draw method)
- -- Events: MouseDownEvent, MouseMoveEvent, MouseUpEvent,basic mouse
- -- event handling
-
- -- Specialized Classes: Stencilizer
-
- -- Instructions to User:
- -- Run this script. When the "ScriptX Stencilizer" window appears,
- -- drag the mouse. An array of ovals creates a red brush stroke along
- -- the mouse path. Change instance variables in the myStencilizer
- -- object to set the brush, stencil, and width of the brush stroke.
-
- -- xx can delete the stencilizer from the window, add new stencilizers,
- -- put the stencilizer in a different window
-
- -- Authors: Jim Inscore and Jocelyn Becker, Kaleida Technical Publications
-
-
- -- <<<
-
- class Stencilizer (TwoDPresenter)
- instance variables
- drawingCache -- bitmap for caching previous strokes
- strokeClass -- class of Stencil used for the current brush
- currentBrush -- Brush instance now used to paint with
-
- strokeWidth -- Half the width of the brush stroke
- mouseDown -- event interests managed by theStencilizer
- mouseMoved
- mouseUp
- end
-
-
-
- method renderStroke self {class Stencilizer} theInterest theEvent ->
- (
- -- get the current mouse point
- local thisPoint := theEvent.localCoords
- local pointx := thisPoint.x
- local pointy := thisPoint.y
- local strokeWidth := self.strokeWidth
-
- -- create a stencil
- local thisStencil := (new self.strokeClass \
- x1:(pointx - strokeWidth)\
- y1:(pointy - strokeWidth) \
- x2:(pointx + strokeWidth) \
- y2:(pointy + strokeWidth))
-
- -- render the stencil to the bitmap surface
- fill self.drawingcache \
- thisStencil \
- self.boundary \
- identityMatrix \
- self.currentBrush
-
- -- call notifyChanged to tell the compositor
- -- that the stencilizer has changed
- notifyChanged self true
-
- -- accept the event
- true
- )
-
-
- -- this method handles mouse down events
- method beginStroke self {class Stencilizer} theInterest theEvent ->
- (
- -- put the mouseMoved and mouseUp events on duty
- addEventInterest self.mouseMoved
- addEventInterest self.mouseUp
-
- -- render the stroke
- renderStroke self theInterest theEvent
- )
-
-
- -- this method handles mouse up events
- method endStroke self {class Stencilizer} theInterest theEvent ->
- (
- -- remove the mouseMoved and mouseUp events from duty
- removeEventInterest self.mouseMoved
- removeEventInterest self.mouseUp
-
- -- render the stroke
- renderStroke self theInterest theEvent
- )
-
- -- this method initializes the Stencilizer
- method init self {class Stencilizer} #rest args ->
- (
- apply nextMethod self args
-
- -- define the brush characteristics
- self.currentBrush := new Brush color:redColor
- self.strokeWidth := 4
- self.strokeClass := Oval
- -- make the drawing cache be fairly big
- self.drawingCache := new BitmapSurface bbox:(new rect x2:600 y2:400)
-
- local theMouseDevice := new MouseDevice
-
- -- set up the mouse event interests
- self.mouseDown := new MouseDownEvent
- self.mouseDown.eventReceiver := beginStroke
- self.mouseDown.authorData := self
- self.mouseDown.device := theMouseDevice
- self.mouseDown.presenter := self
-
- self.mouseMoved := new MouseMoveEvent
- self.mouseMoved.eventReceiver := renderStroke
- self.mouseMoved.authorData := self
- self.mouseMoved.device := theMouseDevice
- self.mouseMoved.presenter := self
-
- self.mouseUp := new MouseUpEvent
- self.mouseUp.eventReceiver := endStroke
- self.mouseUp.authorData := self
- self.mouseUp.device := theMouseDevice
- self.mouseUp.presenter := self
-
- -- make sure stencilizer gets mouse up, even outside its boundary
- self.mouseUp.matchedInterest := self.mouseDown
-
- -- at the beginning, put the mouseDown event on duty
- addEventInterest self.mouseDown
- )
-
-
-
- -- draws the current contents of the Stencilizer to a surface
- -- this method is called automatically by the compositor
- method draw self {class Stencilizer} surface clip ->
- (
- -- transfers the drawing cache to the surface
- transfer surface \
- self.drawingCache \
- self.globalBoundary \
- self.globalTransform
-
- -- draw the outline of the stencilizer's area
- stroke surface \
- self.boundary \
- self.globalBoundary \
- self.globalTransform \
- blackbrush
- )
-
-
-
- -- to save the image created, save the stencilizer
- -- to create a new image, create a new stencilizer
-
- -- sets up a simple example of the stencilizer class
- global myWindow := new Window \
- boundary:(new Rect x2:400 y2:300) \
- name:"ScriptXStencilizer"
-
- global myStencilizer := new Stencilizer \
- boundary:(new Rect x2:400 y2:300)
- append myWindow myStencilizer
-
- myWindow.x := 50
- myWindow.y := 50
- show myWindow
-
-
-
- global redBrush := myStencilizer.currentBrush
- global yellowBrush := new Brush color:yellowColor
- global greenBrush := new Brush color:greenColor
- global blueBrush := new Brush color:blueColor
-
-
- -- try the following changes
-
- -->>>
-
- myStencilizer.strokeWidth := 10
- myStencilizer.strokeClass := Rect
- myStencilizer.currentBrush := yellowBrush
-
-
-
-